package cn.neusoft.service.impl;

import cn.neusoft.mapper.EmployeeMapper;
import cn.neusoft.pojo.Employee;
import cn.neusoft.query.EmployeeQuery;
import cn.neusoft.utils.MyBatisUtils;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.mysql.cj.util.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class EmployeeService implements cn.neusoft.service.IEmployeeService {
	@Override
	public ResultBean add(Employee employee) throws Exception {
		SqlSession sqlSession=null;
		try{
			sqlSession=MyBatisUtils.getSession();
			EmployeeMapper mapper=sqlSession.getMapper(EmployeeMapper.class);
			int count=mapper.add(employee);
			sqlSession.commit();
			return count>0?ResultBean.SUCCESS():ResultBean.ERROR("添加员工信息失败");
		}catch (Exception e) {
			if(sqlSession!=null) {
				sqlSession.rollback();
			}
			return ResultBean.ERROR(e.getMessage());
		} finally {
			if(sqlSession!=null) {
				sqlSession.close();
			}
		}
	}

	@Override
	public ResultBean update(Employee employee) throws Exception {
		SqlSession sqlSession=null;
		try{
			sqlSession=MyBatisUtils.getSession();
			EmployeeMapper mapper=sqlSession.getMapper(EmployeeMapper.class);
			int count=mapper.update(employee);
			sqlSession.commit();
			return count>0?ResultBean.SUCCESS():ResultBean.ERROR("修改员工信息失败");
		}catch (Exception e) {
			if(sqlSession!=null) {
				sqlSession.rollback();
			}
			return ResultBean.ERROR(e.getMessage());
		} finally {
			if(sqlSession!=null) {
				sqlSession.close();
			}
		}
	}

	@Override
	public ResultBean delete(Integer id) throws Exception {
		SqlSession sqlSession=null;
		try{
			sqlSession=MyBatisUtils.getSession();
			EmployeeMapper mapper=sqlSession.getMapper(EmployeeMapper.class);
			int count=mapper.remove(id);
			sqlSession.commit();
			return count>0?ResultBean.SUCCESS():ResultBean.ERROR("删除员工信息失败");
		}catch (Exception e) {
			if(sqlSession!=null) {
				sqlSession.rollback();
			}
			return ResultBean.ERROR(e.getMessage());
		} finally {
			if(sqlSession!=null) {
				sqlSession.close();
			}
		}
	}

	@Override
	public ResultBean getById(Integer id) throws Exception {
		SqlSession sqlSession=null;
		try{
			sqlSession=MyBatisUtils.getSession();
			EmployeeMapper mapper=sqlSession.getMapper(EmployeeMapper.class);
			Employee employee=mapper.getById(id);
			return employee!=null?ResultBean.SUCCESS(employee):ResultBean.ERROR("查询不到该员工信息");
		}catch (Exception e) {
			//出现异常的之后不需要再进行回滚了
			return ResultBean.ERROR(e.getMessage());
		} finally {
			if(sqlSession!=null) {
				sqlSession.close();
			}
		}
	}

	@Override
	public ResultBean getAll() throws Exception {
		SqlSession sqlSession=null;
		try{
			sqlSession=MyBatisUtils.getSession();
			EmployeeMapper mapper=sqlSession.getMapper(EmployeeMapper.class);
			List<Employee> list=mapper.getAll();
			return list!=null?ResultBean.SUCCESS(list):ResultBean.ERROR("查询所有员工失败");
		}catch (Exception e) {
			//出现异常的之后不需要再进行回滚了
			return ResultBean.ERROR(e.getMessage());
		} finally {
			if(sqlSession!=null) {
				sqlSession.close();
			}
		}
	}
	@Override
	public ResultBean getByPage(EmployeeQuery employeeQuery) {
		SqlSession sqlSession=null;
		try{
			sqlSession=MyBatisUtils.getSession();
			EmployeeMapper mapper=sqlSession.getMapper(EmployeeMapper.class);
			PageBean<Employee> pageBean=new PageBean<>();
			int count=mapper.getCount(employeeQuery);
			//在进行分页查询之前要先进行查询总记录数
			List<Employee> list=mapper.getByPage(employeeQuery);
			pageBean.setData(list);
			pageBean.setTotalRows(count);
			return ResultBean.SUCCESS(pageBean);
		}catch (Exception e) {
			//出现异常的之后不需要再进行回滚了
			return ResultBean.ERROR(e.getMessage());
		} finally {
			if(sqlSession!=null) {
				sqlSession.close();
			}
		}
	}

	@Override
	public ResultBean delMany(List<Integer> ids) {
		if(ids==null || ids.size()==0) {
			return ResultBean.ERROR("params is null");
		}
		SqlSession sqlSession = null;
		try{
			sqlSession = MyBatisUtils.getSession();
			EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
			//执行业务
			for (Integer empno:ids){
				int del = mapper.remove(empno);
				if (del==0){
					sqlSession.rollback();
					return ResultBean.ERROR("删除员工信息失败");
				}
			}
			sqlSession.commit();
			return ResultBean.SUCCESS();
		}catch (Exception e){
			//出现异常  回滚
			if (sqlSession!=null){
				sqlSession.rollback();
			}
			return ResultBean.ERROR(e.getMessage());
		}finally {
			//关闭会话
			if (sqlSession!=null){
				sqlSession.close();
			}
		}
	}


	@Override
	public ResultBean addMany(List<Employee> employeeList) {
		if(employeeList==null || employeeList.size()==0) {
			return ResultBean.ERROR("params is null");
		}
		SqlSession sqlSession = null;
		try{
			sqlSession = MyBatisUtils.getSession();
			EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
			//执行业务
			for (Employee emp:employeeList){

				//参数校验  校验每一个员工对象的参数
				if (StringUtils.isNullOrEmpty(emp.getEname())){
					sqlSession.rollback();
					return ResultBean.ERROR("params null");
				}
				int add = mapper.add(emp);
				if (add==0){
					sqlSession.rollback();
					return ResultBean.ERROR("添加员工信息失败");
				}
			}
			sqlSession.commit();
			return ResultBean.SUCCESS();
		}catch (Exception e){
			//出现异常  回滚
			if (sqlSession!=null){
				sqlSession.rollback();
			}
			return ResultBean.ERROR(e.getMessage());
		}finally {
			//关闭会话
			if (sqlSession!=null){
				sqlSession.close();
			}
		}
	}
}
